<template>
    <div class="content-wrap">
        <div class="content-top-box">
            <!-- 搜索区域 -->
            <ul class="search-container">
                <li>
                    <span class="search-item-name">用户名：</span>
                    <el-input
                        class="search-item-content search-item-text"
                        placeholder="请输入用户名"
                        v-model="userName"
                        clearable
                        maxlength="11"
                        @clear="clearUserName"
                    >
                    </el-input>
                </li>
                <li>
                    <span class="search-item-name">操作时间：</span>
                    <el-date-picker
                        v-model="date"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        @change="changeDate">
                    </el-date-picker>
                </li>
                <!-- <li>
                    <span class="search-item-name">操作：</span>
                    <el-select
                        class="search-item-content"
                        v-model="make" 
                        placeholder="请选择"
                        clearable
                        @change="changeMake"
                    >
                        <el-option
                            v-for="item in makeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </li> -->
                <li>
                    <el-button class="search-btn" type="primary" icon="el-icon-search" @click="searchFn" v-if="searchBtn">查询</el-button>
                </li>
            </ul>
        </div>

        <div class="content-bottom-box">
            <!-- 表格 -->
            <div class="content-form-box">
                <el-table
                    v-loading="loadingFlag"
                    :data="tableData"
                    :height="tableHeight"
                    ref="table"
                    border
                    stripe
                    style="width: 100%"
                >
                    <el-table-column
                        prop="serialNumber"
                        label="序号"
                        width="60"
                        :show-overflow-tooltip="true"
                        fixed
                        >
                    </el-table-column>
                    <el-table-column
                        prop="operatorName"
                        label="用户名"
                        width="120"
                        :show-overflow-tooltip="true"
                        fixed
                        >
                    </el-table-column>
                    <el-table-column
                        prop="roleName"
                        label="角色"
                        width="160"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="ip"
                        label="IP"
                        :show-overflow-tooltip="true"
                        width="160"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="module"
                        label="操作"
                        width="160"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="detail"
                        label="内容"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="operateTime"
                        label="操作时间"
                        width="180"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                </el-table>
            </div>

            <!-- 页码 -->
            <div class="content-Page-container">
                <BaseFormPage 
                    :pagination="pagination"
                    @cFirstPageFn="cFirstPageFn"
                    @cLastPageFn="cLastPageFn"
                    @cJumpPageFn="cJumpPageFn"
                    @cPreFn="cPreFn"
                    @cNextFn="cNextFn"
                    @cRefreshFn="cRefreshFn"
                    >
                </BaseFormPage>
            </div>
        </div>
    </div>
</template>

<script>
import Storage from "@/utils/storage";
import BaseFormPage from '@/components/BaseFormPage';
export default {
    components: {
        BaseFormPage
    },
    data() {
        return {
            loadingFlag: false,         //表格数据加载状态
            dialogFlag: false,          //弹窗显示状态
            userName: '',               //姓名
            date: '',                   //注册时间
            startTime: '',              //注册开始时间
            endTime: '',                //注册结束时间
            
            make: '',                   //操作
            makeOptions: [              //操作选项
                {
                    value: '1',
                    label: '登录'
                },
                {
                    value: '2',
                    label: '编辑'
                }
            ],

            tableHeight: 200,           //表格初始高度
            tableData: [],              //表格数据
            detailData: {},             //详情数据
            pageIndex: 1,               //页码
            pageSize: 20,               //每页显示条数
            pagination: {               //分页数据
                loadingFlag: false,     //数据加载状态
                currentPage: 1,         //当前页
                totalPage: 1,           //总页数
                totalNum: 0,            //总条数
                firstNum: 0,            //分页显示起始值
                endNum: 0               //分页显示结束值
            },

            searchBtn: (JSON.parse(Storage.get('roleAuthIdArr'))).indexOf(292) == -1 ? false : true,
        }
    },
    created() {
        this.getListData()
    },
    methods: {
        getListData() { //获取列表信息
            this.loadingFlag = true;
            this.pagination.loadingFlag = true;
            let params = {
                filterValue: this.userName,
                module: this.module,
                startTime: this.startTime,
                endTime: this.endTime,
                pgCt: this.pageIndex,
                pgSz: this.pageSize
            }
            if(!this.userName) {
                delete params.filterValue
            }
            if(!this.date) {
                delete params.startTime
                delete params.endTime
            }

            this.$http.post('/operate_log/list', params)
                .then((res) => {
                    if(res.data.records.length > 0) {
                        this.pagination.totalPage = res.data.pages
                        this.pagination.totalNum = res.data.total
                        
                        res.data.records.forEach((item, index) => {
                            //列表下标
                            item.index = index
                            //列表序号
                            item.serialNumber = ((this.pageIndex - 1) * this.pageSize) + (index + 1)
                            item.startTime   = item.startTime || '--'
                            item.endTime     = item.endTime || '--'
                        })
                        this.pagination.firstNum = res.data.records[0].serialNumber
                        this.pagination.endNum = res.data.records[res.data.records.length-1].serialNumber
                    }else{
                        this.pagination.currentPage = 1
                        this.pagination.totalPage = 1
                        this.pagination.totalNum = 0
                        this.pagination.firstNum = 0
                        this.pagination.endNum = 0
                    }
                    this.tableData  = res.data.records;
                }).then(() => {
                    this.tableHeightFn();
                    this.loadingFlag = false;
                    this.pagination.loadingFlag = false;
                })
        },
        tableHeightFn() { //表格高度
            let tableDataLength = this.tableData.length;
            let minHeight = tableDataLength == 0 ? 0 : 80; //根据需求距离底部的高度
            this.$nextTick(function () {
                //window.innerHeight:浏览器的可用高度
                //this.$refs.table.$el.offsetTop：表格距离浏览器的高度
                this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - minHeight;
                if(this.tableData.length <= 0) {
                    this.tableHeight = '60vh'
                }

                // 通过捕获系统的onresize事件触发去改变原有的高度
                let self = this;
                window.onresize = function() {
                    self.tableHeight = window.innerHeight - self.$refs.table.$el.offsetTop - minHeight;
                    self.tableHeight = self.tableHeight < 200 ? 200 : self.tableHeight;
                }
            })
        },
        resetPageIndex() { //重置页码
            this.pagination.currentPage = 1;
            this.pageIndex = 1;
        },
        clearPhoneFn() { //清空手机号
            this.searchFn()
        },
        clearUserName() { //清空姓名
            this.searchFn()
        },
        changeDate() { //注册时间
            if(this.date) {
                this.startTime = this.date[0]
                this.endTime = this.date[1].replace('00:00:00', '23:59:59')
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.searchFn()
        },
        changeMake() { //是否实名
            this.searchFn()
        },
        changeBindVal() { //是否绑卡
            this.searchFn()
        },
        searchFn() { //搜索
            this.resetPageIndex()
            this.getListData()
        },
        checkUserInfoFn(index) { //查看详情
            this.dialogFlag = true;
            this.detailData = this.tableData[index]
        },
        cFirstPageFn(pageNum) { //首页
            this.pageIndex = pageNum;
            this.getListData()
        },
        cLastPageFn(pageNum) { //尾页
            this.pageIndex = pageNum;
            this.getListData()
        },
        cJumpPageFn(pageNum) { //页码跳转
            this.pageIndex = pageNum;
            this.getListData()
        },
        cPreFn(pageNum) { //上一页
            this.pageIndex = pageNum;
            this.getListData()
        },
        cNextFn(pageNum) { //下一页
            this.pageIndex = pageNum;
            this.getListData()
        },
        resetParams() { //重置参数
            this.phone = ''
            this.userName = ''
            this.registerDate = ''
            this.startCreateTime = ''
            this.endCreateTime = ''
            this.pageIndex = 1
            this.pageSize = 20
            this.realVal = ''
            this.bindVal = ''
            this.tableData = []
            this.detailData = {}
            this.pagination = {
                loadingFlag: false,
                currentPage: 1,
                totalPage: 1,
                totalNum: 0,
                firstNum: 0,
                endNum: 0
            }
        },
        cRefreshFn() { //刷新
            this.resetParams()
            this.getListData()
        }
    }
}
</script>

<style lang="scss" scoped>

</style>